package com.gxa.servlet.activity;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.pojo.Result;
import com.gxa.service.avticity.ActivityService;
import com.gxa.service.avticity.impl.ActivityServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(urlPatterns = "/deleteActivityGoods")
public class DeleteActivityGoodsServlet extends HttpServlet {
    ObjectMapper objectMapper  = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        ActivityService activityService = new ActivityServiceImpl();

        int gid = Integer.parseInt(request.getParameter("gid"));
        System.out.println("gid: "+gid);

        int i = activityService.deleteActivityGoods(gid);

        Map<String,Object> map = new HashMap<>();
        PrintWriter writer = response.getWriter();
        String result = null;
        if (i>0){
            map.put("result", Result.ok("success"));
            result= objectMapper.writeValueAsString(map);
        }else {
            map.put("result",Result.error());
            result= objectMapper.writeValueAsString(map);
        }
        writer.write(result);
        writer.flush();
        writer.close();


    }
}
